package oauth.security;

import lombok.extern.slf4j.Slf4j;
import oauth.domain.SysUser;
import oauth.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Optional;
import java.util.Set;

@Slf4j
public class DomainUserDetailsService implements UserDetailsService
{

	@Autowired
	private SysUserRepository sysUserRepository;

	/**
	 * @param username
	 * @return 注意Optional的使用
	 * @describe
	 * @author zhaocj
	 * @date
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
		throws UsernameNotFoundException
	{
		String lowcaseUsername = username.toLowerCase();
		Optional<SysUser> realUser = sysUserRepository.findOneWithRolesByUsername(lowcaseUsername);

		return realUser.map(user -> {
			Set<GrantedAuthority> grantedAuthorities = user.getAuthorities();
			return new User(user.getUsername(), user.getPassword(), grantedAuthorities);
		}).orElseThrow(() -> new UsernameNotFoundException("用户" + lowcaseUsername + "不存在!"));
	}
}
